﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Gueei.Gallery.Core.Entities;

namespace Gueei.Gallery.Web.ViewModels
{
	public class ImageEditViewModel:ImageViewModel
	{
		private string[] _tags;
		public override string[] Tags
		{
			get { return _tags; }
			set
			{
				_tags = value;
				_tagString = _tags.Aggregate(string.Empty, (s, t) => s += t + ",");
			}
		}

		private string _tagString;
		[RegularExpression(@"[,\s]*([A-Za-z0-9\-]{3,30},+[,\s]*)*[A-Za-z0-9\-]{3,30}[,\s]*")]
		public string TagString
		{
			get { return _tagString; }
			set
			{
				_tagString = value;
				_tags = _tagString.Split(',');
			}
		}

		[RegularExpression(@"^[A-Za-z0-9\-]{3,30}\s*$")]
		public string AddTag { get; set; }

		public new static ImageEditViewModel CreateFromImage(Image image)
		{
			return new ImageEditViewModel {
				Name = image.Name,
				Id = image.Id,
				Description = image.Description,
				Tags = image.Tags.Select(t => t.Name).ToArray(),
				CreateDate = image.CreateDate,
				OriginalFileName = image.OriginalFileName,
				LastModified = image.LastModified
			};
		}
	}
}